﻿using UnityEngine;
using System.Collections.Generic;
using ProtoBuf.Message;

namespace UI
{
    public class UIUnionFightTaskPage : MonoBehaviour
    {
        public GameObject[] tabs;
        public GameObject[] chosed;
        public GameObject pointer;
        public UIScrollView scrollView;
        public SimpleGrid simpleGrid;
        public GameObject itemPrefab;

        public void Init(UIUnionFightRunMap runMapPage)
        {
            gameObject.SetActive(true);
            for (int i = 0; i < tabs.Length; i++)
            {
                tabs[i].GetComponent<UIEventListener>().onClick = SetChosedState;
            }
            SetChosedState(tabs[0]);
            runMapPage.SetRedState(0, false);
            GetComponent<PageTweener>().Play();
        }

        public void UpdateMissionList(List<InstUFUnionMission> list)
        {
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            list.Sort(delegate (InstUFUnionMission a, InstUFUnionMission b)
            {
                if (a.missionIndex > b.missionIndex)
                    return 1;
                else if (a.missionIndex < b.missionIndex)
                    return -1;
                return 0;
            });
            itemPrefab.SetActive(true);
            for (int i = 0; i < list.Count; i++)
            {
                GameObject obj = Instantiate(itemPrefab);
                simpleGrid.AddChild(obj);
                var script = obj.GetComponent<UIUnionFightTaskItem>();
                script.SetItem(list[i]);
            }
            itemPrefab.SetActive(false);
            scrollView.ResetPosition();
        }

        public void UpdatePlayerList(List<InstUFPlayerMission> list)
        {
            list.Sort(delegate (InstUFPlayerMission a, InstUFPlayerMission b)
            {
                if (a.missionIndex > b.missionIndex)
                    return 1;
                else if (a.missionIndex < b.missionIndex)
                    return -1;
                return 0;
            });
            foreach (var item in simpleGrid.childs)
            {
                Destroy(item);
            }
            simpleGrid.childs.Clear();
            itemPrefab.SetActive(true);
            for (int i = 0; i < list.Count; i++)
            {
                GameObject obj = Instantiate(itemPrefab);
                simpleGrid.AddChild(obj);
                var script = obj.GetComponent<UIUnionFightTaskItem>();
                script.SetItem(list[i]);
            }
            itemPrefab.SetActive(false);
            scrollView.ResetPosition();
        }

        public void SetChosedState(GameObject obj)
        {
            for (int i = 0; i < chosed.Length; i++)
            {
                chosed[i].SetActive(false);
            }
            int flag = int.Parse(obj.name);
            chosed[flag].SetActive(true);
            pointer.transform.localPosition = new Vector3(-7.5f, 216 - 60 * flag, 0);
            switch (flag)
            {
                case 0:
                    UIUnionFightRunMap.GetNetData().SendCSUFPlayerMissionList();
                    break;
                case 1:
                    UIUnionFightRunMap.GetNetData().SendCSUFUnionMissionList();
                    break;
                default:
                    Debug.LogError("Error Table Left:" + obj.name);
                    break;
            }
        }

        public void Close()
        {
            gameObject.SetActive(false);
        }

    }
}

